package com.service.impl;

import com.dao.PayDao;
import com.service.PayService;
import com.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * Created by Administrator on 2019/1/10 0010.
 */
@Service
public class PayServiceImpl implements PayService{
    @Autowired
    private PayDao dao;

    @Override
    public String findOpenId(Integer userId,String price,Integer type,String uuid) {
        if(type == 2){
            BigDecimal b = new BigDecimal(price);
            BigDecimal a = new BigDecimal(100);
            BigDecimal prices = b.divide(a, 2, RoundingMode.HALF_UP);
            int count = dao.addReward(userId,prices,uuid);
            if(count > 0){
                return dao.findOpenId(userId);
            }else{
                return "";
            }
        }else{
            return dao.findOpenId(userId);
        }
    }

    @Override
    public String findNewOpenId(Integer userId, String price, Integer type, String uuid) {
        if(type == 2){
            BigDecimal b = new BigDecimal(price);
            BigDecimal a = new BigDecimal(100);
            BigDecimal prices = b.divide(a, 2, RoundingMode.HALF_UP);
            int count = dao.addReward(userId,prices,uuid);
            if(count > 0){
//                return dao.findOpenId(userId);
                return dao.findNewOpenId(userId);
            }else{
                return "";
            }
        }else{
            return dao.findNewOpenId(userId);
        }    }

    @Override
    public int updataRewardMsg(String transaction_id, String out_trade_no, String nonceStr, String payTime) {
        return dao.updataRewardMsg(transaction_id,out_trade_no,nonceStr,payTime);
    }

    @Override
    public int updataPayMessage(String transaction_id, String out_trade_no, String nonceStr, String payTime) {
        return dao.updataPayMessage(transaction_id,out_trade_no,nonceStr,payTime);
    }

    @Override
    public int updataRechargeMsg(String transaction_id, String out_trade_no, String nonceStr, String payTime) {
        return dao.updataRechargeMsg(transaction_id,out_trade_no,nonceStr,payTime);
    }
}
